package Sort;


import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

public class BubbleSort {

	
	public static void main(String[] args) {

		int[] arr = new int[80000];
		for (int i = 0; i < arr.length; i++) {
			arr[i] = (int)(Math.random()*800000);
		}
//		System.out.println("排序前的数组"+Arrays.toString(arr));
		Date date1 = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date1string = simpleDateFormat.format(date1);
		System.out.println("排序前的时间："+date1string);
		
		BubbleSort sort = new BubbleSort();
		sort.bubbleSort(arr);
		
		
		Date date2 = new Date();
		String date2string = simpleDateFormat.format(date2);
		System.out.println("排序后的时间："+date2string);
	}
	//从小到大排序，遇到逆序就停止
	public void bubbleSort(int[] array) {
		if(array.length == 0) {
			return;
		}
		int temp;
		boolean flage = false;//是否交换过(优化1)
		for (int i = 0; i < array.length - 1; i++) {
			flage = false;
//			System.out.printf("第%d趟排序：",i+1);
			for (int j = 0; j < array.length - 1 -i; j++) {
				if(array[j]>array[j+1]) {//遇到逆序，需要交换这两个的位置
					flage = true;
					temp = array[j+1];
					array[j+1] = array[j];
					array[j] = temp;
				}
			}
//			System.out.println(Arrays.toString(array));
			if(!flage) {
				return;
			}
		}
	}
}
